package com.itheima.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.itheima.Code;
import com.itheima.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(LoggerFactory.class);

    @Autowired
    private UserService userService;

    @GetMapping("/findAll")
    public Result findAll(){
        List<User> userList = userService.findAll();

        if (userList != null){
            return new Result(Code.OK,userList,"查询成功");
        }else{
            return new Result(Code.ERROR,"查询失败");
        }
    }




    @GetMapping("/findByIds")
    public List<User> findByIds(){
        List list = new ArrayList();
        list.add(1);
        list.add(3);
       return userService.findByIds(list);
    }

    /*文件上传*/
    @RequestMapping("/fileUpload")
    public void fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
     /*   Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(2020,11,7,17,0,0);
        long millis = calendar.getTimeInMillis();
        System.out.println(millis);*/

     /*   long milliSecond = 1607331600000L;
        Date date = new Date();
        date.setTime(milliSecond);
        System.out.println(new SimpleDateFormat().format(date));*/

        //首先判断是否为空文件,大小是否为0字节
        if (!file.isEmpty()) {
            //获取文件名称
            String oldFileName = file.getOriginalFilename();
            //设置保存的路径
            String realPath = request.getServletContext().getRealPath("/images");

            //System.out.println(oldFileName);
            //System.out.println(realPath);
            //
        if (!new File(realPath).exists()){
            //如果文件不存在,创建
            new File(realPath).mkdirs();
        }

        //得到原始文件的后缀
            String suffix = oldFileName.substring(oldFileName.lastIndexOf("."));
        //文件名称,uuid
            String prefix = UUID.randomUUID().toString().replaceAll("-", "");
        //新的文件名
            String newFileName = prefix+suffix;

            file.transferTo(new File(realPath,newFileName));

        }

    }

    //批量修改性别为女   即性别状态码为2
    @RequestMapping("/updateGenders")
    public void updateGenders(){
        List ids = new ArrayList();
        userService.updateGenders(ids);
    }
    @RequestMapping("/userDetail")
    @JsonView(User.Userdetail.class)
    public List<User> userdetail(){
        return userService.findAll();
    }

    @RequestMapping("/userInfo")
    @JsonView(User.UserInfo.class)
    public List<User> userInfo(){
        List<User> all = userService.findAll();
        return all;
    }
}
